//index.js
//获取应用实例
var app = getApp()
var index = {
  data: {
    mobile: '...',
    userInfo: {},
    dt:'-'
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  toBind: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  init:function(){
    var that = this
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 100000
    })
    wx.login({
      success: function (res) {
        if (res.code) {
          //统一下单接口对接
          wx.request({
            url: 'https://www.eddieby.top/gd/public/index.php/index/index/get_session',
            data: {
              code: res.code
            },
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (response) {
              wx.setStorage({
                key: "openid",
                data: response.data.openid
              })
              wx.setStorage({
                key: "session_key",
                data: response.data.session_key
              })
              wx.setStorage({
                key: "mobile",
                data: response.data.mobile
              })
              if (response.data.mobile == 0) {
                wx.getSystemInfo({
                  success: function (res) {
                    if (that.cpr('6.5.6', res.version)){
                      wx.showModal({
                        title: '请先绑定手机',
                        content: '为了更好使用狗迪，请先绑定手机',
                        success: function (res) {
                          if (res.confirm) {
                            wx.navigateTo({
                              url: '../mobile/mobile'
                            })
                          }
                        }
                      })
                     }else{
                      wx.showModal({
                        title: '请先升级微信',
                        content: '为了更好使用狗迪，请先升级微信',
                        success: function (res) {
                          if (res.confirm) {
                            that.init()
                          } else if (res.cancel) {
                            that.init()
                          }
                        }
                      })
                     }
                    console.log()
                  }
                })
              }else{
                if (((response.data.dt - Math.round(Date.now() / 1000)) / 86400) <= 1 && (response.data.dt - Math.round(Date.now() / 1000))>0) {
                  that.setData({
                    mobile: response.data.mobile,
                    userinfo: response.data,
                    dt: Math.round(((response.data.dt - Math.round(Date.now() / 1000))/ 86400)*24)+'小时后过期'
                  })
                } else if (((response.data.dt - Math.round(Date.now() / 1000)) / 86400) > 1 && (response.data.dt - Math.round(Date.now() / 1000)) > 0){
                  that.setData({
                    mobile: response.data.mobile,
                    userinfo: response.data,
                    dt:Math.round((response.data.dt - Math.round(Date.now() / 1000)) / 86400)+ '天后过期'
                  })
                } else if ((response.data.dt - Math.round(Date.now() / 1000))<=0){
                  that.setData({
                    mobile: response.data.mobile,
                    userinfo: response.data,
                    dt: '已过期'
                  })
                }
              }
            }
          });
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
    wx.getUserInfo({
      success: function (res) {
        if (res.userInfo) {
          wx.setStorage({
            key: 'nickName',
            data: res.userInfo.nickName,
          })
          wx.setStorage({
            key: 'avatarUrl',
            data: res.userInfo.avatarUrl,
          })
          that.setData({
            userInfo: res.userInfo
          })
          wx.hideToast()
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
  },
  cpr: function (currVerArr, promoteVerArr){
    var currVer = currVerArr || "0.0.0";
    var promoteVer = promoteVerArr || "0.0.0";
    if (currVer == promoteVer) return true;
    var currVerArr = currVer.split(".");
    var promoteVerArr = promoteVer.split(".");
    var len = Math.max(currVerArr.length, promoteVerArr.length);
    for (var i = 0; i < len; i++) {
      var proVal = ~~promoteVerArr[i],curVal = ~~currVerArr[i];
      if (proVal < curVal) {
        return false;
        break
      } else if (proVal > curVal) {
        return true;
        break
      }
    }
  },
  onShow: function () {
   this.init()
  },
  toBuy: function (event) {
  var that = this
  if (wx.getStorageSync('mobile')==0){
    wx.showModal({
      title: '请先绑定手机',
      content: '购买套餐前请先绑定手机',
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '../mobile/mobile'
          })
        }
      }
    })
  }else{
    wx.request({
      url: 'https://www.eddieby.top/gd/public/index.php/index/index/pay',
      data: {
        openid: wx.getStorageSync('openid'),
        fee: event.currentTarget.dataset.num * 100,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (response) {
        that.setData({
          motto: 'success'
        })
        console.log(response.data)
        // 发起支付
        wx.requestPayment({
          'timeStamp': response.data.timeStamp,
          'nonceStr': response.data.nonceStr,
          'package': response.data.package,
          'signType': 'MD5',
          'paySign': response.data.paySign,
          'success': function (res) {
            wx.showToast({
              title: '支付成功'
            });
            // console.log(res);
          }
        });
      }
    });
  }
  }
}
Page(index)
